//
//  ZYXTitleButton.m
//  Lottery
//  合买跟单控制器 导航栏的标题按钮
//

#import "ZYXTitleButton.h"

@interface ZYXTitleButton()

/**
 *  合买跟单控制器 导航栏的标题按钮 字体
 */
@property (nonatomic,strong) UIFont *titleFont;

@end

@implementation ZYXTitleButton


/**
 *  从xib storyboard 文件中解析出视图对象
 */
-(id)initWithCoder:(NSCoder *)aDecoder
{
    //NSLog(@"initWithCoder---------");
    
    if(self = [super initWithCoder:aDecoder]){
        [self setup];
    }
    return self;
}



/**
 *  通过代码创建该类的对象
 */
-(id)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]){
        [self setup];
    }
    return self;
}



/**
 *  初始化
 */
-(void)setup
{
    self.titleFont = [UIFont systemFontOfSize:15];
    self.titleLabel.font = self.titleFont;
    
    //按钮小图片在按钮的imageView居中显示
    self.imageView.contentMode = UIViewContentModeCenter;
}



-(void)awakeFromNib
{
    //NSLog(@"awakeFromNib---------");
//    self.titleFont = [UIFont systemFontOfSize:15];
//    self.titleLabel.font = self.titleFont;
//    
//    //按钮小图片在按钮的imageView居中显示
//    self.imageView.contentMode = UIViewContentModeCenter;
    
}


/*
-(void)awakeFromNib
{
    self.titleFont = self.titleLabel.font;
    
    //Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]'
    //First throw call stack:
    //(
}
*/

/*
-(void)willMoveToSuperview:(UIView *)newSuperview
{
    self.titleFont = self.titleLabel.font;
    
    //[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]
}
*/


/**
 *  按钮的标题的尺寸
 *
 *  @param contentRect 按钮的bounds
 *
 *  @return 按钮内部标题的宽高
 */
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    //NSLog(@"titleRectForContentRect---------");
    
    CGFloat titleX = 0;
    CGFloat titleY = 0;
    
    //按照文字的个数来计算文字的宽度 拿到当前按钮的文字 计算这行文字的总的宽度
    //以下代码陷入死循环
    //拿到按钮的 self.titleLabel 属性 回调用 按钮的 - (CGRect)titleRectForContentRect:(CGRect)contentRect 方法 | 死循环错误
    //NSDictionary *attrs = @{NSFontAttributeName : self.titleLabel.font};
    
    NSDictionary *attrs = @{NSFontAttributeName : self.titleFont};
    
    CGFloat titleW = 0;
    

    if(iOS7){
#ifdef __IPHONE_7_0 //如果你的Xcode版本 >= 5 才编译下句代码
        titleW = [self.currentTitle boundingRectWithSize:CGSizeMake(MAXFLOAT,MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size.width;
#else
        titleW = [self.currentTitle sizeWithFont:self.titleFont].width;
#endif
    }else{
        titleW = [self.currentTitle sizeWithFont:self.titleFont].width;
    }
    
    CGFloat titleH = contentRect.size.height;
    
    return CGRectMake(titleX,titleY,titleW,titleH);
}


/**
 *  按钮的小图片的尺寸
 *
 *  @param contentRect 按钮的bounds
 *
 *  @return 按钮内部小图片imageView的宽高
 */
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    CGFloat imageW = 30;
    CGFloat imageX = contentRect.size.width - imageW;
    CGFloat imageY = 0;
    CGFloat imageH = contentRect.size.height;
    
    return CGRectMake(imageX,imageY,imageW,imageH);
}

@end
